package com.xuecheng.learning.controller;

import com.xuecheng.api.learning.CourseRecordApi;
import com.xuecheng.api.learning.model.dto.CourseRecordDTO;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.learning.common.utils.UAASecurityUtil;
import com.xuecheng.learning.service.CourseRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 选课记录 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class CourseRecordController implements CourseRecordApi {

    @Autowired
    private CourseRecordService courseRecordService;

    /**
     * 查询该学习进度
     *
     * @param coursePubId 课程发布ID
     * @return
     */
    @GetMapping("learnedRecords/myCourseRec/{coursePubId}")
    public CourseRecordDTO getRecordByCoursePubId(@PathVariable Long coursePubId) {
        String username = UAASecurityUtil.getUser().getUsername();
        CourseRecordDTO dto = courseRecordService.getRecordByCoursePubId(coursePubId, username);
        return dto;
    }

    /**
     * 订单支付后创建默认学习记录
     *
     * @param username    用户名称
     * @param coursePubId 课程发布ID
     */
    @PostMapping("l/course-record/paid/{username}/{coursePubId}")
    public RestResponse<CourseRecordDTO> createCourseRecord4s(@PathVariable String username, @PathVariable Long coursePubId) {
        return courseRecordService.createCourseRecord4s(username, coursePubId);
    }


}
